﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using QLKhachSan.DTO;
using System.Data.SqlClient;
using QLKhachSan.DAO;
using QLKhachSan.BUS;
using DevExpress.XtraEditors;

namespace QLKhachSan.GUI
{
    public partial class fKhachHang : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public fKhachHang()
        {
            InitializeComponent();
        }

        private void fKhachHang_Load(object sender, EventArgs e)
        {
            dataGridViewKH.DataSource = BUS_KhachHang.hienthiDanhSachKhachHangDaThue();
            simpleButtonHuyTK.Enabled = false;
            simpleButtonOK.Enabled = false;
            simpleButtonTK.Enabled = true;
            radioButtonMaKH.Checked = true;            
            radioButtonCMND.Enabled = false;
            radioButtonDiaChi.Enabled = false;
            radioButtonMaKH.Enabled = false;
            radioButtonMaSoThue.Enabled = false;
            radioButtonNgayThanhToan.Enabled = false;
            radioButtonNgayThue.Enabled = false;
            radioButtonQuocTich.Enabled = false;
            radioButtonSDT.Enabled = false;
            radioButtonTenKH.Enabled = false;
            radioButtonTienPaiTra.Enabled = false;
            radioButtonTienTra.Enabled = false;
            textBoxTimKiem.Enabled = false;
            textBoxPaiDen.ResetText();
            textBoxPaitu.Enabled = false;
            textBoxPaiDen.Enabled = false;
            textBoxTraTu.Enabled = false;
            textBoxPaitu.ResetText();
            textBoxTraDen.Enabled = false;
            textBoxTraDen.ResetText();
            textBoxTraTu.ResetText();
            textBoxTimKiem.Enabled = false;
            textBoxTimKiem.Font = new System.Drawing.Font("Times New Roman", 9.75F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            textBoxTimKiem.ForeColor = System.Drawing.SystemColors.WindowFrame;
            textBoxTimKiem.Text = "Nhập Từ Khóa Tìm Kiếm...";
            dateTimePickerNgayDen.Enabled = false;
            dateTimePickerNgayTu.Enabled = false;
            dateTimePickerTTDen.Enabled = false;
            dateTimePickerTTTu.Enabled = false;
        }

        private void textBoxTimKiem_MouseClick(object sender, MouseEventArgs e)
        {
            if (textBoxTimKiem.Text.Trim() == "Nhập Từ Khóa Tìm Kiếm...")
            {
                textBoxTimKiem.Font = new System.Drawing.Font("Times New Roman", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                textBoxTimKiem.ForeColor = System.Drawing.SystemColors.WindowText;
                textBoxTimKiem.ResetText();
            }
        }

        private void radioButtonTienPaiTra_MouseClick(object sender, MouseEventArgs e)
        {
            textBoxPaiDen.Enabled = true;
            textBoxPaitu.Enabled = true;
            textBoxTimKiem.Enabled = false;
            textBoxTimKiem.Font = new System.Drawing.Font("Times New Roman", 9.75F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            textBoxTimKiem.ForeColor = System.Drawing.SystemColors.WindowFrame;
            textBoxTimKiem.Text = "Nhập Từ Khóa Tìm Kiếm...";
        }

        private void radioButtonNgayThue_MouseClick(object sender, MouseEventArgs e)
        {
            dateTimePickerNgayDen.Enabled = true;
            dateTimePickerNgayTu.Enabled = true;
            textBoxTimKiem.Enabled = false;
            textBoxTimKiem.Font = new System.Drawing.Font("Times New Roman", 9.75F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            textBoxTimKiem.ForeColor = System.Drawing.SystemColors.WindowFrame;
            textBoxTimKiem.Text = "Nhập Từ Khóa Tìm Kiếm...";
        }

        private void dateTimePickerTTTu_ValueChanged(object sender, EventArgs e)
        {
            dateTimePickerTTDen.MinDate = dateTimePickerTTTu.Value;
        }

        private void dateTimePickerNgayTu_ValueChanged(object sender, EventArgs e)
        {
            dateTimePickerNgayDen.MinDate = dateTimePickerNgayTu.Value;

        }

        private void radioButtonNgayThanhToan_MouseClick(object sender, MouseEventArgs e)
        {
            dateTimePickerTTDen.Enabled = true;
            dateTimePickerTTTu.Enabled = true;
            textBoxTimKiem.Enabled = false;       
            textBoxTimKiem.Font = new System.Drawing.Font("Times New Roman", 9.75F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            textBoxTimKiem.ForeColor = System.Drawing.SystemColors.WindowFrame;
            textBoxTimKiem.Text = "Nhập Từ Khóa Tìm Kiếm...";
        }

        private void radioButtonTienTra_MouseClick(object sender, MouseEventArgs e)
        {
            textBoxTraDen.Enabled = true;            
            textBoxTraTu.Enabled = true;
        }

        private void textBoxTraTu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && (e.KeyChar != '.'))
                e.Handled = true;
        }

        private void textBoxTraDen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && (e.KeyChar != '.'))
                e.Handled = true;
        }

        private void textBoxPaitu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && (e.KeyChar != '.'))
                e.Handled = true;
        }

        private void textBoxPaiDen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && (e.KeyChar != '.'))
                e.Handled = true;
        }

        private void simpleButtonHuyTK_Click(object sender, EventArgs e)
        {
            dataGridViewKH.DataSource = BUS_KhachHang.hienthiDanhSachKhachHangDaThue();
            textBoxTimKiem.Enabled = false;
            simpleButtonHuyTK.Enabled = false;
            simpleButtonOK.Enabled = false;
            simpleButtonTK.Enabled = true;
            radioButtonMaKH.Checked = true;
            radioButtonCMND.Enabled = false;
            radioButtonDiaChi.Enabled = false;
            radioButtonMaKH.Enabled = false;
            radioButtonNgayThue.Enabled = false;
            radioButtonMaSoThue.Enabled = false;
            radioButtonNgayThanhToan.Enabled = false;
            radioButtonQuocTich.Enabled = false;
            radioButtonSDT.Enabled = false;
            radioButtonTenKH.Enabled = false;
            radioButtonTienPaiTra.Enabled = false;
            radioButtonTienTra.Enabled = false;
            textBoxPaiDen.ResetText();
            textBoxPaitu.Enabled = false;
            textBoxPaiDen.Enabled = false;
            textBoxTraTu.Enabled = false;
            textBoxPaitu.ResetText();
            textBoxTraDen.Enabled = false;
            textBoxTraDen.ResetText();
            textBoxTraTu.ResetText();         
                textBoxTimKiem.Enabled = true;           
            textBoxTimKiem.Font = new System.Drawing.Font("Times New Roman", 9.75F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            textBoxTimKiem.ForeColor = System.Drawing.SystemColors.WindowFrame;
            textBoxTimKiem.Text = "Nhập Từ Khóa Tìm Kiếm...";
            dateTimePickerNgayDen.Enabled = false;
            dateTimePickerNgayTu.Enabled = false;
            dateTimePickerTTDen.Enabled = false;
            dateTimePickerTTTu.Enabled = false;
        }

        private void simpleButtonTK_Click(object sender, EventArgs e)
        {
            textBoxTimKiem.Enabled = false ;
            simpleButtonHuyTK.Enabled = true;
            simpleButtonOK.Enabled = true;
            simpleButtonTK.Enabled = false;            
            radioButtonMaKH.Checked = true;            
            radioButtonCMND.Enabled = true;
            radioButtonDiaChi.Enabled = true;
            radioButtonMaKH.Enabled = true;
            radioButtonMaSoThue.Enabled = true;
            radioButtonNgayThanhToan.Enabled = true;
            radioButtonQuocTich.Enabled = true;
            radioButtonSDT.Enabled = true;
            radioButtonTenKH.Enabled = true;
            radioButtonTienPaiTra.Enabled = true;
            radioButtonTienTra.Enabled = true;
            textBoxPaiDen.ResetText();
            textBoxPaitu.Enabled =false;
            textBoxPaiDen.Enabled = false;
            textBoxPaitu.ResetText();
            textBoxTraTu.Enabled = false;
            
            textBoxTraDen.Enabled = false;
            textBoxTraDen.ResetText();
            textBoxTraTu.ResetText();
            textBoxTimKiem.Enabled = true;
            radioButtonNgayThue.Enabled = true;
            textBoxTimKiem.Font = new System.Drawing.Font("Times New Roman", 9.75F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            textBoxTimKiem.ForeColor = System.Drawing.SystemColors.WindowFrame;
            textBoxTimKiem.Text = "Nhập Từ Khóa Tìm Kiếm...";
        }

        private void radioButtonMaKH_MouseClick(object sender, MouseEventArgs e)
        {
            textBoxTimKiem.Enabled = true;
        }

        private void radioButtonTenKH_MouseClick(object sender, MouseEventArgs e)
        {
            textBoxTimKiem.Enabled = true;
        }

        private void radioButtonCMND_MouseClick(object sender, MouseEventArgs e)
        {
            textBoxTimKiem.Enabled = true;
        }

        private void radioButtonDiaChi_MouseClick(object sender, MouseEventArgs e)
        {
            textBoxTimKiem.Enabled = true;
        }

        private void radioButtonSDT_MouseClick(object sender, MouseEventArgs e)
        {
            textBoxTimKiem.Enabled = true;
        }

        private void radioButtonQuocTich_MouseClick(object sender, MouseEventArgs e)
        {
            textBoxTimKiem.Enabled = true;
        }

        private void radioButtonMaSoThue_MouseClick(object sender, MouseEventArgs e)
        {
            textBoxTimKiem.Enabled = true;
        }

        private void radioButtonTienPaiTra_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonTienPaiTra.Checked == false)
            {
                textBoxPaiDen.ResetText();
                textBoxPaitu.Enabled = false;
                textBoxPaiDen.Enabled = false;
                textBoxPaitu.ResetText();
            }
        }

        private void radioButtonTienTra_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonTienTra.Checked == false)
            {
                textBoxTraTu.Enabled = false;
                textBoxTraDen.Enabled = false;
                textBoxTraDen.ResetText();
                textBoxTraTu.ResetText();
            }
        }

        private void radioButtonNgayThue_CheckedChanged(object sender, EventArgs e)
        {
            if(radioButtonNgayThue.Checked == false)
            {
                dateTimePickerNgayDen.Enabled = false;
                dateTimePickerNgayTu.Enabled = false;

            }
        }

        private void radioButtonNgayThanhToan_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonNgayThanhToan.Checked == false)
            {
                dateTimePickerTTDen.Enabled = false;
                dateTimePickerTTTu.Enabled = false;
            }
        }

        private void simpleButtonOK_Click(object sender, EventArgs e)
        {
            if (radioButtonCMND.Checked == true)
            {
                if (textBoxTimKiem.Text.Trim() != "")
                {
                    dataGridViewKH.DataSource = DAO_KhachHang.hienthiDanhSachKhachHangTheoCMND(textBoxTimKiem.Text.ToString());
               
                }
                else
                {
                    MessageBox.Show("Bạn Chưa Nhập Thông Tin Tìm Kiếm!!!???", "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            if (radioButtonDiaChi.Checked == true)
            {
                if (textBoxTimKiem.Text.Trim() != "")
                {
                    dataGridViewKH.DataSource = clsKetNoi.TimKiem("pr_HienThiDanhSachKhachHangDaThue", "DiaChi", textBoxTimKiem.Text.Trim());
                 
                }
                else
                {
                    MessageBox.Show("Bạn Chưa Nhập Thông Tin Tìm Kiếm!!!???", "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            if (radioButtonMaKH.Checked == true)
            {
                if (textBoxTimKiem.Text.Trim() != "")
                {
                    dataGridViewKH.DataSource = DAO_KhachHang.hienthiDanhSachKhachHangTheoMaKH(int.Parse(textBoxTimKiem.Text.ToString()));
                   
                }
                else
                {
                    MessageBox.Show("Bạn Chưa Nhập Thông Tin Tìm Kiếm!!!???", "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            if (radioButtonMaSoThue.Checked == true)
            {
                if (textBoxTimKiem.Text.Trim() != "")
                {
                    dataGridViewKH.DataSource = DAO_KhachHang.hienthiDanhSachKhachHangTheoMaSoThue(textBoxTimKiem.Text.ToString());
                    
                }
                else
                {
                    MessageBox.Show("Bạn Chưa Nhập Thông Tin Tìm Kiếm!!!???", "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            if (radioButtonNgayThanhToan.Checked == true)
            {
                dataGridViewKH.DataSource = DAO_KhachHang.hienthiDanhSachKhachHangKhoangNgayThanhToan(DateTime.Parse(dateTimePickerTTTu.Text.ToString()),DateTime.Parse(dateTimePickerTTDen.Text.ToString()));
                
            }
            if (radioButtonNgayThue.Checked == true)
            {
                dataGridViewKH.DataSource = DAO_KhachHang.hienthiDanhSachKhachHangKhoangNgayThue(DateTime.Parse(dateTimePickerNgayTu.Text.ToString()), DateTime.Parse(dateTimePickerNgayDen.Text.ToString()));
                            
            }
            if (radioButtonQuocTich.Checked == true)
            {
                if (textBoxTimKiem.Text.Trim() != "")
                {
                    dataGridViewKH.DataSource = clsKetNoi.TimKiem("pr_HienThiDanhSachKhachHangDaThue", "QuocTich", textBoxTimKiem.Text.ToString().Trim());
                }
                else
                {
                    MessageBox.Show("Bạn Chưa Nhập Thông Tin Tìm Kiếm!!!???", "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            if (radioButtonSDT.Checked == true)
            {
                if (textBoxTimKiem.Text.Trim() != "")
                {
                    dataGridViewKH.DataSource = DAO_KhachHang.hienthiDanhSachKhachHangTheoSDT(textBoxTimKiem.Text.ToString());
                }
                else
                {
                    MessageBox.Show("Bạn Chưa Nhập Thông Tin Tìm Kiếm!!!???", "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            if (radioButtonTenKH.Checked == true)
            {
                if (textBoxTimKiem.Text.Trim() != "")
                {
                    dataGridViewKH.DataSource = clsKetNoi.TimKiem("pr_HienThiDanhSachKhachHangDaThue", "TenKH", textBoxTimKiem.Text.ToString().Trim());
                }
                else
                {
                    MessageBox.Show("Bạn Chưa Nhập Thông Tin Tìm Kiếm!!!???", "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            } 
            if (radioButtonTienPaiTra.Checked == true)
            {
                if (textBoxPaiDen.Text.Trim() != "" && textBoxPaitu.Text.Trim() !="")
                {
                    if(int.Parse(textBoxPaiDen.Text) >= int.Parse(textBoxPaitu.Text))
                    {
                        dataGridViewKH.DataSource = clsKetNoi.TimKiemKhoang("pr_HienThiDanhSachKhachHangDaThue", "TienPhaiTra",textBoxPaitu.Text.ToString(),textBoxPaiDen.Text.ToString());
                    }
                    else
                    {
                        MessageBox.Show("Bạn Nhập Thông Tin Từ Đến Không Hợp Lệ (Đến Lớn Hơn Hoặc Bằng Từ)!!!???", "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("Bạn Chưa Nhập Thông Tin Tìm Kiếm!!!???", "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            if (radioButtonTienTra.Checked == true)
            {
                if (textBoxPaiDen.Text.Trim() != "" && textBoxPaitu.Text.Trim() != "")
                {
                    if (int.Parse(textBoxPaiDen.Text) >= int.Parse(textBoxPaitu.Text))
                    {
                        dataGridViewKH.DataSource = clsKetNoi.TimKiemKhoang("pr_HienThiDanhSachKhachHangDaThue", "TienTraTruoc", textBoxTraTu.Text.ToString(), textBoxTraDen.Text.ToString());
                    }
                    else
                    {
                        MessageBox.Show("Bạn TNhập Thông Tin Từ Đến Không Hợp Lệ (Đến Lớn Hơn Hoặc Bằng Từ)!!!???", "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("Bạn Chưa Nhập Thông Tin Tìm Kiếm!!!???", "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
           
        }

        private void textBoxTimKiem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(radioButtonCMND.Checked == true || radioButtonSDT.Checked == true || radioButtonMaKH.Checked == true ||radioButtonMaSoThue.Checked == true )
            {
                  if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && (e.KeyChar != '.'))
                    e.Handled = true;
            }
        }

        private void simpleButtonThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       
    }
}